package com.mutual.system.areaInfo.service.impl;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.mutual.appUserInfo.service.AppUserInfoService;
import com.mutual.commom.pojo.ReturnResult;
import com.mutual.system.areaInfo.dao.AreaInfoDao;
import com.mutual.system.areaInfo.pojo.AreaInfo;
import com.mutual.system.areaInfo.service.AreaInfoService;
import com.mutual.system.user.service.UserService;

@Service("areaInfoService")
public class AreaInfoServiceImpl implements AreaInfoService{

	@Resource
	private AreaInfoDao areaInfoDao;
	
	@Resource
	private UserService userService;
	
	@Resource
	private AppUserInfoService appUserInfoService;
	
	@Override
	public List<AreaInfo> findAreaInfoList(AreaInfo info) {
		return areaInfoDao.selectObjectList(info);
	}

	@Override
	public void insert(AreaInfo entity) {
		Date nowDate=new Date();
		entity.setCreateDate(nowDate);
		entity.setUpdateDate(nowDate);
		Integer maxCode=areaInfoDao.selectMaxCode(entity.getPId());
		if(maxCode==null){
			entity.setCode(entity.getParentCode()+"01");
		}else{
			entity.setCode(String.valueOf(maxCode+1));
		}
		areaInfoDao.insertSelective(entity);
	}

	@Override
	public void updateByPrimaryKey(AreaInfo entity) {
		entity.setUpdateDate(new Date());
		areaInfoDao.updateByPrimaryKeySelective(entity);
	}

	@Override
	public AreaInfo selectByPrimaryKey(Serializable entityid) {
		return areaInfoDao.selectByPrimaryKey(entityid);
	}

	@Override
	public ReturnResult deleteAreaInfo(AreaInfo areaInfo) {
		ReturnResult result=new ReturnResult();
		Map<String,Object> param=Maps.newHashMap();
		param.put("areaCode", areaInfo.getCode());
		int userCount=userService.selectUserListCount(param);
		if(userCount>0){
			result.setStatus(ReturnResult.STATUS_FAILED);
			result.setReturnMsg("删除失败，存在后台用户数据");
			return result;
		}
		int appUserCount=appUserInfoService.selectAppUserInfoCount(param);
		if(appUserCount>0){
			result.setStatus(ReturnResult.STATUS_FAILED);
			result.setReturnMsg("删除失败，存在app用户数据");
			return result;
		}
		areaInfoDao.deleteByPrimaryKey(areaInfo.getId());
		result.setStatus(ReturnResult.STATUS_SUCCESS);
		result.setReturnMsg("删除成功");
		return result;
	}
}
